package src.week7.SHIYAN4.JavaSocket;

/**
 * @Author： 20192315裴湘瑞
 * @Date: 2020/10/27 19:05
 * @Version: 1.0
 * @Target：编写客户端程序
 * @Requirement：
 */

import java.awt.BorderLayout;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;

import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;

/*客户端窗口构建*/
public class ClientChatView extends JFrame{
    //显示文本的窗口
    private JTextArea ta = new JTextArea(10,20);
    //输入文本的窗口
    private JTextField tf = new JTextField(20);

    //创建客户端socket
    private Socket cs = null;
    //指定客户端socket端口
    private static final int PORT = 8888;
    //指定客户端socket的ip地址
    private static final String CONNSTR = "192.168.56.1";
    //网络输出流
    private DataOutputStream dos = null;
    private boolean isconn = false;

    //ClientChatView构造方法
    public ClientChatView() throws HeadlessException {
        super();
    }
    //ClientChatView初始化方法
    public void init() {
        //设置窗口标题
        this.setTitle("ClientView");
        //设置窗口样式
        this.add(ta,BorderLayout.CENTER);
        this.add(tf,BorderLayout.SOUTH);
        //设置窗口总大小
        this.setBounds(300, 300, 300, 400);
        //关闭上边窗口输入功能
        ta.setEditable(false);
        //光标聚焦
        tf.requestFocus();
        //点击窗口叉号=关闭客户端
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //监听tf输入到ta,并清除掉tf中内容
        tf.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                //获取tf中输入的信息
                String strSend = tf.getText();
                //使用send方法发送信息给服务器端
                send(strSend);
                if (strSend.trim().length()==0) {
                    return;//tf中没有输入
                }
                tf.setText("");
                //加入到ta中
                //ta.append(strSend+"\n");
            }
        });


        //正式创建客户端Socket
        /*try catch处理异常*/
        try {
            cs = new Socket(CONNSTR,PORT);
            isconn = true;
        } catch (UnknownHostException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        this.setVisible(true);
        new Thread(new Receive()).start();
    }

    //发送信息到服务器上的方法
    public void send(String str) {
        try {
            //使用socket的getinputstream方法获取网络字节输入流对象
            dos = new DataOutputStream(cs.getOutputStream());
            //将输出流信息发送给服务器
            dos.writeUTF(str);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
    //多线程的类，实现多线程接收
    class Receive implements Runnable{
        public void run() {
            try {
                while(isconn) {
                    DataInputStream dis = new DataInputStream(cs.getInputStream());
                    String str = dis.readUTF();
                    ta.append(str);
                }
            }catch (SocketException e3) {
                System.out.println("Sever stopped");
                ta.append("Server stopped");
            }
            catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    public static void main(String[] args) {
        ClientChatView cc = new ClientChatView();
        cc.init();
    }
}
